home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Lib / format / rfc2UTC.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  5.4 KB  |  262 lines

  1. /* rfc2UTC.c - Converts an RFC string into UTC structure */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Lib/format/RCS/rfc2UTC.c,v 6.0 1991/12/18 20:22:06 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Lib/format/RCS/rfc2UTC.c,v 6.0 1991/12/18 20:22:06 jpo Rel $
  9.  *
  10.  * $Log: rfc2UTC.c,v $
  11.  * Revision 6.0  1991/12/18  20:22:06  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16.  
  17.  
  18. #include "util.h"
  19. #include <isode/cmd_srch.h>
  20. #include "mta.h"
  21.  
  22.  
  23. extern CMD_TABLE        tbl_month[];
  24.  
  25. /* ---------------------  Begin  Routines  -------------------------------- */
  26.  
  27.  
  28.  
  29.  
  30. /*
  31. This routine converts a RFC 822 standard time string like:
  32.     "Tue, 16 Aug 88 10:23:21 BST"
  33. into a UTC structure. Where "Tue," is optional.
  34. */
  35.  
  36. static void getzone ();
  37.  
  38. int rfc2UTC (str, utc)
  39. char    *str;
  40. UTC     *utc;
  41. {
  42.     UTC     ut;
  43.     char    *cp, *cp2;
  44.     char    tbuf[LINESIZE];
  45.     int     n;
  46.  
  47.  
  48.     PP_DBG (("Lib/rfc2UTC (%s)", str));
  49.  
  50.     ut = (UTC) smalloc (sizeof *ut);
  51.     bzero ((char *)ut, sizeof *ut);
  52.     *utc = NULL;
  53.  
  54.     cp = str;
  55. #define skips(c)        while (isspace (*c)) c++
  56. #define skipsp(c)       while (isspace (*c) || ispunct(*c)) c++;
  57.  
  58.     skips (cp);
  59.  
  60.     if (isalpha (*cp)) {
  61.         while(isalpha (*cp))
  62.             cp ++;
  63.         skipsp (cp);
  64.     }
  65.  
  66.  
  67.     /* month day */
  68.     if (isdigit (*cp)) {
  69.         n = 0;
  70.         while (isdigit (*cp))
  71.             n = n * 10 + (*cp++ - '0');
  72.         ut -> ut_mday = n;
  73.         skips (cp);
  74.     }
  75.     else {
  76.         PP_LOG (LLOG_EXCEPTIONS, ("Bad month day '%s'", str));
  77.         free((char *)ut);
  78.         return NOTOK;
  79.     }
  80.  
  81.     /* Month */
  82.     if (isalpha (*cp)) {
  83.         for (cp2 = tbuf; isalpha(*cp); *cp2 ++ = *cp++)
  84.             continue;
  85.         *cp2 = NULL;
  86.         if (cp2 - tbuf > 3)
  87.             tbuf[3] = NULL;
  88.         ut -> ut_mon = cmd_srch (tbuf, tbl_month);
  89.         if (ut -> ut_mon == -1) {
  90.             free ((char *)ut);
  91.             return NOTOK;
  92.         }
  93.         ut -> ut_mon ++;
  94.         skips (cp);
  95.     }
  96.     else    {
  97.         free((char*)ut);
  98.         return NOTOK;
  99.     }
  100.  
  101.     /* -- Year -- */
  102.     if (isdigit (*cp)) {
  103.         n = 0;
  104.         while (isdigit (*cp))
  105.             n = n * 10 + (*cp ++ - '0');
  106.         if (n < 100)
  107.             ut -> ut_year = 1900 + n;
  108.         else
  109.             ut -> ut_year = n;
  110.         skips (cp);
  111.     }
  112.     else {
  113.         free ((char *)ut);
  114.         return NOTOK;
  115.     }
  116.  
  117.     if (isdigit (*cp)) {
  118.         n = 0;
  119.         while (isdigit(*cp))
  120.             n = n * 10 + (*cp++ - '0');
  121.         ut -> ut_hour = n;
  122.         skips (cp);
  123.     }
  124.  
  125.     /* -- Minute & Secs -- */
  126.     if (*cp == ':') {
  127.         cp ++;
  128.         n = 0;
  129.         while (isdigit (*cp))
  130.             n = n * 10 + (*cp++ - '0');
  131.         ut -> ut_min = n;
  132.         skips (cp);
  133.     }
  134.     if (*cp == ':') {
  135.         cp ++;
  136.         n = 0;
  137.         while (isdigit (*cp))
  138.             n = n * 10 + (*cp ++ - '0');
  139.         ut -> ut_sec = n;
  140.         ut -> ut_flags |= UT_SEC;
  141.         skips (cp);
  142.     }
  143.  
  144.     getzone (cp, ut);
  145.  
  146.     *utc = ut;
  147.     PP_DBG (("Lib/rfc2UTC returns (%.22s)", utct2str (ut)));
  148.  
  149.     return OK;
  150. }
  151.  
  152.  
  153.  
  154. static CMD_TABLE        zone_tbl[] = {  /* some of the more popular zones... */
  155.     "A",    -100,
  156.     "AHST",    -1000,    /* Alaska-Hawaii Std Time */
  157.     "AT",    -200,    /* Azores Time */
  158.     "AST",    -400,    /* Atlantic Std Time */
  159.     "B",    -200,
  160.     "BST",  100,    /* British Summer Time, also Brasil Std Time (-300) and Bering Strait Time (-1100) */
  161.     "BT",    -1100,    /* Bering Time also Baghdad Time (+200) */
  162.     "C",    -300,
  163.     "CAT",    -1000,    /* Central Alaska Time */
  164.     "CCT",    800,    /* China Coast Time, USSR Zone 7 */
  165.     "CDT",  -500,
  166.     "CET",    100,    /* Central European Time */
  167.     "CST",  -600,    /* Central Std Time */
  168.     "D",    -400,
  169.     "E",    -500,
  170.     "EDT",  -400,
  171.     "EET",    200,    /* Eastern European Time, USSR Zone 1 */
  172.     "EST",  -500,    /* Eastern Std Time */
  173.     "F",    -600,
  174.     "G",    -700,
  175.     "GMT",  0,
  176.     "GST",    1000,    /* Guam Std Time, USSR Zone 9 */
  177.     "H",    -800,
  178.     "HST",    -1030,    /* Hawaiian Std Time */
  179.     "I",    -900,
  180.     "IDLE",    1200,    /* International Date Line, East */
  181.     "IDLW",    -1200,    /* International Date Line, West */
  182.     "IST",    530,    /* Indian Standard Time */
  183.     "IT",    300,    /* Iran Time */
  184.     "JST",    900,    /* Japan Std Time, USSR Zone 8 */
  185.     "JT",    730,    /* Java Time */
  186.     "K",    -1000,
  187.     "L",    -1100,
  188.     "M",    -1200,
  189.     "MDT",  -600,
  190.     "MET",    100,    /* Middle European Time */
  191.     "MST",  -700,    /* Mountain Std Time */
  192.     "MT",    830,    /* Moluccas Time */
  193.     "N",    100,
  194.     "NT",    -1100,    /* Nome Time */
  195.         "NFT",    -330,    /* Newfoundland Time */
  196.     "NST",    630,    /* North Sumatra Time */
  197.     "NZT",    1130,    /* New Zealand Time */
  198.     "O",    200,
  199.     "P",    300,
  200.     "PDT",  -700,
  201.     "PST",  -800,    /* Paciific Std Time */
  202.     "Q",    400,
  203.     "R",    500,
  204.     "S",    600,
  205.     "SAST",    930,    /* South Australia Std Time */
  206.     "SST",    700,    /* South Sumatra Time */
  207.     "T",    700,
  208.     "U",    800,
  209.     "UT",   0,
  210.     "V",    900,
  211.     "W",    1000,
  212.     "WAT",    -100,    /* West Africa Time */
  213.     "WDT",  100,
  214.     "WET",  0,    /* Western European Time */
  215.     "X",    1100,
  216.     "Y",    1200,
  217.     "YST",    -900,    /* Yukon Std Time */
  218.     "Z",    0,
  219.     0,      -99
  220. };
  221. #define N_ZONES ((sizeof(zone_tbl)/sizeof(CMD_TABLE)) - 1)
  222.  
  223. static void getzone (bp, ut)
  224. char    *bp;
  225. UTC     ut;
  226. {
  227.     int     hours, mins;
  228.     char    *cp, zbuf[64];
  229.  
  230.     while (isspace (*bp))
  231.         bp ++;
  232.  
  233.     if (*bp == '+' || *bp == '-') { /* offset TZ */
  234.         hours = (bp[1] - '0') * 10 + (bp[2] - '0');
  235.         mins = (bp[3] - '0') * 10 + (bp[4] - '0');
  236.         ut -> ut_zone = 60 * hours + mins;
  237.         if (*bp == '-')
  238.             ut -> ut_zone = - ut -> ut_zone;
  239.  
  240.         ut -> ut_flags |= UT_ZONE;
  241.         return;
  242.     }
  243.     if (isalpha(*bp)) {     /* 'real' TZ (e.g. BST) */
  244.         for (cp = zbuf; isalpha(*bp); )
  245.               *cp++ = *bp ++;
  246.         *cp = 0;
  247.         if ((hours = cmdbsrch (zbuf, zone_tbl, N_ZONES)) != -99) {
  248.             ut -> ut_flags |= UT_ZONE;
  249.             ut -> ut_zone = ((hours/100) * 60);
  250.             /* add minutes */
  251.             if (hours >= 0)
  252.                 ut -> ut_zone += (hours%100);
  253.             else
  254.                 ut -> ut_zone -= (hours%100);
  255.             return;
  256.         }
  257.         PP_LOG (LLOG_EXCEPTIONS, ("Unknown timezone syntax %s", zbuf));
  258.     }
  259.     if (*bp != '\0')
  260.         PP_LOG (LLOG_EXCEPTIONS, ("Unknown timezone syntax %s", bp));
  261. }
  262.